from app import db
import json

from flask import Blueprint, render_template, request, redirect, url_for

from app.models import Announcement

# 查询分页数据
import decimal

import time


class DecimalEncoder(json.JSONEncoder):

    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return float(o)
        super(DecimalEncoder, self).default(o)


bp = Blueprint('noticeOp', __name__, url_prefix='/notice')

@bp.route('/list', methods=['GET', 'POST'])
def list():
    # 调用查询分页数据服务方法

    notices = Announcement.query.all()
    context = {'detail': notices}
    # 返回结果
    return render_template('notice.html', **context)


@bp.route('/modify', methods=['GET', 'POST'])
def modify():
    time = request.form['time']
    notices = Announcement.query.filter_by(time=time).all()
    print(notices)
    noticeLists = [notice.to_dict() for notice in notices]
    print(noticeLists)
    return json.dumps(noticeLists, cls=DecimalEncoder)


@bp.route('/modifyResult', methods=['PUT'])
def modifyResult():

    time = request.form['time']
    title = request.form['title']
    content = request.form['content']

    example = Announcement.query.filter_by(time=time).first()
    example.title = title
    example.content = content
    db.session.add(example)
    db.session.commit()

    notices = Announcement.query.all()
    print(notices)
    noticeLists = [notice.to_dict() for notice in notices]
    return json.dumps(noticeLists, cls=DecimalEncoder)


# 查询通知详情
@bp.route('/detail', methods=['GET', 'POST'])
def detail():
    # 调用查询通知详情服务方法
    notices = Announcement.query.all()
    noticeList = [notice.to_dict() for notice in notices]
    # 返回结果
    return json.dumps(noticeList, cls=DecimalEncoder)


# 添加通知
@bp.route('/add', methods=['GET', 'POST'])
def add():
    #  调用添加通知服务
    # result = notice.NoticeAdd()
    # # 返回结果
    # return result
    
    if request.method == 'GET':
        return render_template('edit.html')
    else:
        print("here in line 83")
        time = request.form['time']
        title = request.form['title']
        content = request.form['content']
        content1 = Announcement(time=time, title=title, content=content)
        db.session.add(content1)
        db.session.commit()
    return redirect(url_for('noticeOp.list'))


@bp.route('/delete', methods=["DELETE"])
def delete():

    time = request.form['time']
    example = Announcement.query.filter_by(time=time).first()

    print(example)
    db.session.delete(example)
    db.session.commit()
    notices = Announcement.query.all()
    print(notices)
    noticeLists = [announcement.to_dict() for announcement in notices]
    print(noticeLists)
    return json.dumps(noticeLists, cls=DecimalEncoder)

@bp.route('/search', methods=['GET', 'POST'])
def search():
    data = request.args.get('search')

    #模糊查询
    example = Announcement.query.filter(Announcement.title.like(f'%{data}%')).all()

    context = {
        'detail': example
    }
    return render_template('notice.html', **context)

@bp.route('/returnNotice', methods=['GET', 'POST'])
def returnNotice():
    announcements = Announcement.query.all()
    announcementLists = [user.to_dict() for user in announcements]
    return json.dumps(announcementLists,cls=DecimalEncoder)
